package main

import (
	"fmt"
	"os"
	_ "time"
)

import (
	"buyChance"
	dbc "dbConnector"
	"monitor"
	"r_log"
	"recom"
	"resource"
	"schedule"
	_ "tp"
	"util"
	"version"
)

var logSwitch = "Info"

func main() {
	//fmt.Println(1 << 11)
	fmt.Println(version.GetVersion())
	// create log
	err := r_log.Init("stockrecom", logSwitch, "../log", false, "midnight", 365)
	if err != nil {
		fmt.Printf("main(): err in log.Init():%s\n", err.Error())
		os.Exit(-1)
	}
	//初始化redis
	// err = dbc.Init("../conf/redisConf")
	// if err != nil {
	// 	r_log.Logger.Error(err)
	// 	Exit(-1)
	// }
	// r_log.Logger.Info("init redis success")
	err = util.InitUtil("../conf/utilConf")
	if err != nil {
		r_log.Logger.Error(err)
		Exit(-1)
	}
	r_log.Logger.Info("init Util success")

	//初始化msdb
	err = dbc.UpdateServerList("../conf/serverList")
	if err != nil {
		r_log.Logger.Error(err)
		Exit(-1)
	}
	r_log.Logger.Info("init serverlist success")

	//初始化TPS结构
	// tradingPosition.InitTradingPosition()
	// r.Tps = &tradingPosition.TradingPositions
	// r_log.Logger.Info("init tps success")
	//初始化资源
	r := new(recom.Recommender)
	r.Res, err = resource.NewResource()
	if err != nil {
		r_log.Logger.Error(err)
		Exit(-1)
	}
	r_log.Logger.Info("init res success")
	//加载当日已分配的股票信息
	err = r.Init()
	if err != nil {
		r_log.Logger.Error(err)
		Exit(-1)
	}
	r_log.Logger.Info("init recom result success")

	err = buyChance.InitModeType("../conf/ModeType")
	if err != nil {
		r_log.Logger.Error(err)
		Exit(-1)
	}
	//fmt.Println(buyChance.MODEMASK_CHAODIE)
	r_log.Logger.Info("init ModeType success")

	//初始化buyChance和滤网
	err = buyChance.Init("../conf/ddsConf", r.Res)
	if err != nil {
		r_log.Logger.Error(err)
		Exit(-1)
	}
	r.Bcs = &buyChance.BCBase
	r_log.Logger.Info("init bcs success")

	//初始化monitor
	err = monitor.StartMonitor("../conf/monitorConf", r)
	if err != nil {
		r_log.Logger.Error(err)
	}
	r_log.Logger.Info("start monitor server success")
	// //初始时间表
	ss, err := schedule.InitScheduleServer("../conf/SecheduleSocketConf", r)
	if err != nil {
		r_log.Logger.Error(err)
		Exit(-1)
	}
	r_log.Logger.Info("init schedule success")

	//启动定时推荐
	ss.Run()
}

func Exit(n int) {
	r_log.Logger.Close()
	r_log.BCLogger.Close()
	r_log.RecomLogger.Close()
	r_log.TPSLogger.Close()
	os.Exit(n)
}
